package com.javaweb.blogcite.controller;

import com.javaweb.blogcite.aspect.JwtCheck;
import com.javaweb.blogcite.common.ResponseResult;
import com.javaweb.blogcite.pojo.Banner;
import com.javaweb.blogcite.service.BannerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class BannerController {

    @Resource
    private BannerService bannerService;

    @GetMapping("/api/banner")
    public ResponseResult<List<Banner>> getBanners() {
        List<Banner> banners = bannerService.listAllBanners();
        ResponseResult<List<Banner>> responseResult = new ResponseResult<>();
        responseResult.setCode(0);
        responseResult.setMsg("success");
        responseResult.setData(banners);

        return responseResult;
    }

    @JwtCheck
    @PostMapping("/api/banner")
    public ResponseResult<List<Banner>> setBanners(@RequestBody List<Banner> bannerList) {
        boolean success = bannerService.updateBanner(bannerList);
        ResponseResult<List<Banner>> responseResult = new ResponseResult<>();
        if (success) {
            responseResult.setCode(0);
            responseResult.setMsg("success");
        } else {
            responseResult.setCode(500);
            responseResult.setMsg("失败");
        }
        responseResult.setData(bannerList);

        return responseResult;
    }
}
